package dict_pb

import (
	context "context"
	fmt "fmt"
	gorm1 "github.com/infobloxopen/atlas-app-toolkit/gorm"
	query "github.com/infobloxopen/atlas-app-toolkit/query"
	errors "github.com/infobloxopen/protoc-gen-gorm/errors"
	gorm "github.com/jinzhu/gorm"
	field_mask "google.golang.org/genproto/protobuf/field_mask"
	timestamppb "google.golang.org/protobuf/types/known/timestamppb"
	strings "strings"
	time "time"
)

type DictDetailORM struct {
	CreatedAt *time.Time
	DictId    uint64
	Id        uint64
	Label     string
	Sort      uint64
	Status    bool
	UpdatedAt *time.Time
	Value     uint64
}

// TableName overrides the default tablename generated by GORM
func (DictDetailORM) TableName() string {
	return "dict_details"
}

// ToORM runs the BeforeToORM hook if present, converts the fields of this
// object to ORM format, runs the AfterToORM hook, then returns the ORM object
func (m *DictDetail) ToORM(ctx context.Context) (DictDetailORM, error) {
	to := DictDetailORM{}
	var err error
	if prehook, ok := interface{}(m).(DictDetailWithBeforeToORM); ok {
		if err = prehook.BeforeToORM(ctx, &to); err != nil {
			return to, err
		}
	}
	to.Id = m.Id
	if m.CreatedAt != nil {
		t := m.CreatedAt.AsTime()
		to.CreatedAt = &t
	}
	if m.UpdatedAt != nil {
		t := m.UpdatedAt.AsTime()
		to.UpdatedAt = &t
	}
	to.Label = m.Label
	to.Value = m.Value
	to.Status = m.Status
	to.Sort = m.Sort
	to.DictId = m.DictId
	if posthook, ok := interface{}(m).(DictDetailWithAfterToORM); ok {
		err = posthook.AfterToORM(ctx, &to)
	}
	return to, err
}

// ToPB runs the BeforeToPB hook if present, converts the fields of this
// object to PB format, runs the AfterToPB hook, then returns the PB object
func (m *DictDetailORM) ToPB(ctx context.Context) (DictDetail, error) {
	to := DictDetail{}
	var err error
	if prehook, ok := interface{}(m).(DictDetailWithBeforeToPB); ok {
		if err = prehook.BeforeToPB(ctx, &to); err != nil {
			return to, err
		}
	}
	to.Id = m.Id
	if m.CreatedAt != nil {
		to.CreatedAt = timestamppb.New(*m.CreatedAt)
	}
	if m.UpdatedAt != nil {
		to.UpdatedAt = timestamppb.New(*m.UpdatedAt)
	}
	to.Label = m.Label
	to.Value = m.Value
	to.Status = m.Status
	to.Sort = m.Sort
	to.DictId = m.DictId
	if posthook, ok := interface{}(m).(DictDetailWithAfterToPB); ok {
		err = posthook.AfterToPB(ctx, &to)
	}
	return to, err
}

// The following are interfaces you can implement for special behavior during ORM/PB conversions
// of type DictDetail the arg will be the target, the caller the one being converted from

// DictDetailBeforeToORM called before default ToORM code
type DictDetailWithBeforeToORM interface {
	BeforeToORM(context.Context, *DictDetailORM) error
}

// DictDetailAfterToORM called after default ToORM code
type DictDetailWithAfterToORM interface {
	AfterToORM(context.Context, *DictDetailORM) error
}

// DictDetailBeforeToPB called before default ToPB code
type DictDetailWithBeforeToPB interface {
	BeforeToPB(context.Context, *DictDetail) error
}

// DictDetailAfterToPB called after default ToPB code
type DictDetailWithAfterToPB interface {
	AfterToPB(context.Context, *DictDetail) error
}

// DefaultCreateDictDetail executes a basic gorm create call
func DefaultCreateDictDetail(ctx context.Context, in *DictDetail, db *gorm.DB) (*DictDetail, error) {
	if in == nil {
		return nil, errors.NilArgumentError
	}
	ormObj, err := in.ToORM(ctx)
	if err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(DictDetailORMWithBeforeCreate_); ok {
		if db, err = hook.BeforeCreate_(ctx, db); err != nil {
			return nil, err
		}
	}
	if err = db.Create(&ormObj).Error; err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(DictDetailORMWithAfterCreate_); ok {
		if err = hook.AfterCreate_(ctx, db); err != nil {
			return nil, err
		}
	}
	pbResponse, err := ormObj.ToPB(ctx)
	return &pbResponse, err
}

type DictDetailORMWithBeforeCreate_ interface {
	BeforeCreate_(context.Context, *gorm.DB) (*gorm.DB, error)
}
type DictDetailORMWithAfterCreate_ interface {
	AfterCreate_(context.Context, *gorm.DB) error
}

func DefaultReadDictDetail(ctx context.Context, in *DictDetail, db *gorm.DB, fs *query.FieldSelection) (*DictDetail, error) {
	if in == nil {
		return nil, errors.NilArgumentError
	}
	ormObj, err := in.ToORM(ctx)
	if err != nil {
		return nil, err
	}
	if ormObj.Id == 0 {
		return nil, errors.EmptyIdError
	}
	if hook, ok := interface{}(&ormObj).(DictDetailORMWithBeforeReadApplyQuery); ok {
		if db, err = hook.BeforeReadApplyQuery(ctx, db, fs); err != nil {
			return nil, err
		}
	}
	if db, err = gorm1.ApplyFieldSelection(ctx, db, fs, &DictDetailORM{}); err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(DictDetailORMWithBeforeReadFind); ok {
		if db, err = hook.BeforeReadFind(ctx, db, fs); err != nil {
			return nil, err
		}
	}
	ormResponse := DictDetailORM{}
	if err = db.Where(&ormObj).First(&ormResponse).Error; err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormResponse).(DictDetailORMWithAfterReadFind); ok {
		if err = hook.AfterReadFind(ctx, db, fs); err != nil {
			return nil, err
		}
	}
	pbResponse, err := ormResponse.ToPB(ctx)
	return &pbResponse, err
}

type DictDetailORMWithBeforeReadApplyQuery interface {
	BeforeReadApplyQuery(context.Context, *gorm.DB, *query.FieldSelection) (*gorm.DB, error)
}
type DictDetailORMWithBeforeReadFind interface {
	BeforeReadFind(context.Context, *gorm.DB, *query.FieldSelection) (*gorm.DB, error)
}
type DictDetailORMWithAfterReadFind interface {
	AfterReadFind(context.Context, *gorm.DB, *query.FieldSelection) error
}

func DefaultDeleteDictDetail(ctx context.Context, in *DictDetail, db *gorm.DB) error {
	if in == nil {
		return errors.NilArgumentError
	}
	ormObj, err := in.ToORM(ctx)
	if err != nil {
		return err
	}
	if ormObj.Id == 0 {
		return errors.EmptyIdError
	}
	if hook, ok := interface{}(&ormObj).(DictDetailORMWithBeforeDelete_); ok {
		if db, err = hook.BeforeDelete_(ctx, db); err != nil {
			return err
		}
	}
	err = db.Where(&ormObj).Delete(&DictDetailORM{}).Error
	if err != nil {
		return err
	}
	if hook, ok := interface{}(&ormObj).(DictDetailORMWithAfterDelete_); ok {
		err = hook.AfterDelete_(ctx, db)
	}
	return err
}

type DictDetailORMWithBeforeDelete_ interface {
	BeforeDelete_(context.Context, *gorm.DB) (*gorm.DB, error)
}
type DictDetailORMWithAfterDelete_ interface {
	AfterDelete_(context.Context, *gorm.DB) error
}

func DefaultDeleteDictDetailSet(ctx context.Context, in []*DictDetail, db *gorm.DB) error {
	if in == nil {
		return errors.NilArgumentError
	}
	var err error
	keys := []uint64{}
	for _, obj := range in {
		ormObj, err := obj.ToORM(ctx)
		if err != nil {
			return err
		}
		if ormObj.Id == 0 {
			return errors.EmptyIdError
		}
		keys = append(keys, ormObj.Id)
	}
	if hook, ok := (interface{}(&DictDetailORM{})).(DictDetailORMWithBeforeDeleteSet); ok {
		if db, err = hook.BeforeDeleteSet(ctx, in, db); err != nil {
			return err
		}
	}
	err = db.Where("id in (?)", keys).Delete(&DictDetailORM{}).Error
	if err != nil {
		return err
	}
	if hook, ok := (interface{}(&DictDetailORM{})).(DictDetailORMWithAfterDeleteSet); ok {
		err = hook.AfterDeleteSet(ctx, in, db)
	}
	return err
}

type DictDetailORMWithBeforeDeleteSet interface {
	BeforeDeleteSet(context.Context, []*DictDetail, *gorm.DB) (*gorm.DB, error)
}
type DictDetailORMWithAfterDeleteSet interface {
	AfterDeleteSet(context.Context, []*DictDetail, *gorm.DB) error
}

// DefaultStrictUpdateDictDetail clears / replaces / appends first level 1:many children and then executes a gorm update call
func DefaultStrictUpdateDictDetail(ctx context.Context, in *DictDetail, db *gorm.DB) (*DictDetail, error) {
	if in == nil {
		return nil, fmt.Errorf("Nil argument to DefaultStrictUpdateDictDetail")
	}
	ormObj, err := in.ToORM(ctx)
	if err != nil {
		return nil, err
	}
	lockedRow := &DictDetailORM{}
	db.Model(&ormObj).Set("gorm:query_option", "FOR UPDATE").Where("id=?", ormObj.Id).First(lockedRow)
	if hook, ok := interface{}(&ormObj).(DictDetailORMWithBeforeStrictUpdateCleanup); ok {
		if db, err = hook.BeforeStrictUpdateCleanup(ctx, db); err != nil {
			return nil, err
		}
	}
	if hook, ok := interface{}(&ormObj).(DictDetailORMWithBeforeStrictUpdateSave); ok {
		if db, err = hook.BeforeStrictUpdateSave(ctx, db); err != nil {
			return nil, err
		}
	}
	if err = db.Save(&ormObj).Error; err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(DictDetailORMWithAfterStrictUpdateSave); ok {
		if err = hook.AfterStrictUpdateSave(ctx, db); err != nil {
			return nil, err
		}
	}
	pbResponse, err := ormObj.ToPB(ctx)
	if err != nil {
		return nil, err
	}
	return &pbResponse, err
}

type DictDetailORMWithBeforeStrictUpdateCleanup interface {
	BeforeStrictUpdateCleanup(context.Context, *gorm.DB) (*gorm.DB, error)
}
type DictDetailORMWithBeforeStrictUpdateSave interface {
	BeforeStrictUpdateSave(context.Context, *gorm.DB) (*gorm.DB, error)
}
type DictDetailORMWithAfterStrictUpdateSave interface {
	AfterStrictUpdateSave(context.Context, *gorm.DB) error
}

// DefaultPatchDictDetail executes a basic gorm update call with patch behavior
func DefaultPatchDictDetail(ctx context.Context, in *DictDetail, updateMask *field_mask.FieldMask, db *gorm.DB) (*DictDetail, error) {
	if in == nil {
		return nil, errors.NilArgumentError
	}
	var pbObj DictDetail
	var err error
	if hook, ok := interface{}(&pbObj).(DictDetailWithBeforePatchRead); ok {
		if db, err = hook.BeforePatchRead(ctx, in, updateMask, db); err != nil {
			return nil, err
		}
	}
	pbReadRes, err := DefaultReadDictDetail(ctx, &DictDetail{Id: in.GetId()}, db, nil)
	if err != nil {
		return nil, err
	}
	pbObj = *pbReadRes
	if hook, ok := interface{}(&pbObj).(DictDetailWithBeforePatchApplyFieldMask); ok {
		if db, err = hook.BeforePatchApplyFieldMask(ctx, in, updateMask, db); err != nil {
			return nil, err
		}
	}
	if _, err := DefaultApplyFieldMaskDictDetail(ctx, &pbObj, in, updateMask, "", db); err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&pbObj).(DictDetailWithBeforePatchSave); ok {
		if db, err = hook.BeforePatchSave(ctx, in, updateMask, db); err != nil {
			return nil, err
		}
	}
	pbResponse, err := DefaultStrictUpdateDictDetail(ctx, &pbObj, db)
	if err != nil {
		return nil, err
	}
	if hook, ok := interface{}(pbResponse).(DictDetailWithAfterPatchSave); ok {
		if err = hook.AfterPatchSave(ctx, in, updateMask, db); err != nil {
			return nil, err
		}
	}
	return pbResponse, nil
}

type DictDetailWithBeforePatchRead interface {
	BeforePatchRead(context.Context, *DictDetail, *field_mask.FieldMask, *gorm.DB) (*gorm.DB, error)
}
type DictDetailWithBeforePatchApplyFieldMask interface {
	BeforePatchApplyFieldMask(context.Context, *DictDetail, *field_mask.FieldMask, *gorm.DB) (*gorm.DB, error)
}
type DictDetailWithBeforePatchSave interface {
	BeforePatchSave(context.Context, *DictDetail, *field_mask.FieldMask, *gorm.DB) (*gorm.DB, error)
}
type DictDetailWithAfterPatchSave interface {
	AfterPatchSave(context.Context, *DictDetail, *field_mask.FieldMask, *gorm.DB) error
}

// DefaultPatchSetDictDetail executes a bulk gorm update call with patch behavior
func DefaultPatchSetDictDetail(ctx context.Context, objects []*DictDetail, updateMasks []*field_mask.FieldMask, db *gorm.DB) ([]*DictDetail, error) {
	if len(objects) != len(updateMasks) {
		return nil, fmt.Errorf(errors.BadRepeatedFieldMaskTpl, len(updateMasks), len(objects))
	}

	results := make([]*DictDetail, 0, len(objects))
	for i, patcher := range objects {
		pbResponse, err := DefaultPatchDictDetail(ctx, patcher, updateMasks[i], db)
		if err != nil {
			return nil, err
		}

		results = append(results, pbResponse)
	}

	return results, nil
}

// DefaultApplyFieldMaskDictDetail patches an pbObject with patcher according to a field mask.
func DefaultApplyFieldMaskDictDetail(ctx context.Context, patchee *DictDetail, patcher *DictDetail, updateMask *field_mask.FieldMask, prefix string, db *gorm.DB) (*DictDetail, error) {
	if patcher == nil {
		return nil, nil
	} else if patchee == nil {
		return nil, errors.NilArgumentError
	}
	var err error
	var updatedCreatedAt bool
	var updatedUpdatedAt bool
	for i, f := range updateMask.Paths {
		if f == prefix+"Id" {
			patchee.Id = patcher.Id
			continue
		}
		if !updatedCreatedAt && strings.HasPrefix(f, prefix+"CreatedAt.") {
			if patcher.CreatedAt == nil {
				patchee.CreatedAt = nil
				continue
			}
			if patchee.CreatedAt == nil {
				patchee.CreatedAt = &timestamppb.Timestamp{}
			}
			childMask := &field_mask.FieldMask{}
			for j := i; j < len(updateMask.Paths); j++ {
				if trimPath := strings.TrimPrefix(updateMask.Paths[j], prefix+"CreatedAt."); trimPath != updateMask.Paths[j] {
					childMask.Paths = append(childMask.Paths, trimPath)
				}
			}
			if err := gorm1.MergeWithMask(patcher.CreatedAt, patchee.CreatedAt, childMask); err != nil {
				return nil, nil
			}
		}
		if f == prefix+"CreatedAt" {
			updatedCreatedAt = true
			patchee.CreatedAt = patcher.CreatedAt
			continue
		}
		if !updatedUpdatedAt && strings.HasPrefix(f, prefix+"UpdatedAt.") {
			if patcher.UpdatedAt == nil {
				patchee.UpdatedAt = nil
				continue
			}
			if patchee.UpdatedAt == nil {
				patchee.UpdatedAt = &timestamppb.Timestamp{}
			}
			childMask := &field_mask.FieldMask{}
			for j := i; j < len(updateMask.Paths); j++ {
				if trimPath := strings.TrimPrefix(updateMask.Paths[j], prefix+"UpdatedAt."); trimPath != updateMask.Paths[j] {
					childMask.Paths = append(childMask.Paths, trimPath)
				}
			}
			if err := gorm1.MergeWithMask(patcher.UpdatedAt, patchee.UpdatedAt, childMask); err != nil {
				return nil, nil
			}
		}
		if f == prefix+"UpdatedAt" {
			updatedUpdatedAt = true
			patchee.UpdatedAt = patcher.UpdatedAt
			continue
		}
		if f == prefix+"Label" {
			patchee.Label = patcher.Label
			continue
		}
		if f == prefix+"Value" {
			patchee.Value = patcher.Value
			continue
		}
		if f == prefix+"Status" {
			patchee.Status = patcher.Status
			continue
		}
		if f == prefix+"Sort" {
			patchee.Sort = patcher.Sort
			continue
		}
		if f == prefix+"DictId" {
			patchee.DictId = patcher.DictId
			continue
		}
	}
	if err != nil {
		return nil, err
	}
	return patchee, nil
}

// DefaultListDictDetail executes a gorm list call
func DefaultListDictDetail(ctx context.Context, db *gorm.DB, f *query.Filtering, s *query.Sorting, p *query.Pagination, fs *query.FieldSelection) ([]*DictDetail, error) {
	in := DictDetail{}
	ormObj, err := in.ToORM(ctx)
	if err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(DictDetailORMWithBeforeListApplyQuery); ok {
		if db, err = hook.BeforeListApplyQuery(ctx, db, f, s, p, fs); err != nil {
			return nil, err
		}
	}
	db, err = gorm1.ApplyCollectionOperators(ctx, db, &DictDetailORM{}, &DictDetail{}, f, s, p, fs)
	if err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(DictDetailORMWithBeforeListFind); ok {
		if db, err = hook.BeforeListFind(ctx, db, f, s, p, fs); err != nil {
			return nil, err
		}
	}
	db = db.Where(&ormObj)
	db = db.Order("id")
	ormResponse := []DictDetailORM{}
	if err := db.Find(&ormResponse).Error; err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(DictDetailORMWithAfterListFind); ok {
		if err = hook.AfterListFind(ctx, db, &ormResponse, f, s, p, fs); err != nil {
			return nil, err
		}
	}
	pbResponse := []*DictDetail{}
	for _, responseEntry := range ormResponse {
		temp, err := responseEntry.ToPB(ctx)
		if err != nil {
			return nil, err
		}
		pbResponse = append(pbResponse, &temp)
	}
	return pbResponse, nil
}

type DictDetailORMWithBeforeListApplyQuery interface {
	BeforeListApplyQuery(context.Context, *gorm.DB, *query.Filtering, *query.Sorting, *query.Pagination, *query.FieldSelection) (*gorm.DB, error)
}
type DictDetailORMWithBeforeListFind interface {
	BeforeListFind(context.Context, *gorm.DB, *query.Filtering, *query.Sorting, *query.Pagination, *query.FieldSelection) (*gorm.DB, error)
}
type DictDetailORMWithAfterListFind interface {
	AfterListFind(context.Context, *gorm.DB, *[]DictDetailORM, *query.Filtering, *query.Sorting, *query.Pagination, *query.FieldSelection) error
}
type DictDetailServiceDefaultServer struct {
	DB *gorm.DB
}

// Create ...
func (m *DictDetailServiceDefaultServer) Create(ctx context.Context, in *CreateDictDetailRequest) (*CreateDictDetailResponse, error) {
	db := m.DB
	if custom, ok := interface{}(in).(DictDetailServiceDictDetailWithBeforeCreate); ok {
		var err error
		if db, err = custom.BeforeCreate(ctx, db); err != nil {
			return nil, err
		}
	}
	res, err := DefaultCreateDictDetail(ctx, in.GetPayload(), db)
	if err != nil {
		return nil, err
	}
	out := &CreateDictDetailResponse{Result: res}
	if custom, ok := interface{}(in).(DictDetailServiceDictDetailWithAfterCreate); ok {
		var err error
		if err = custom.AfterCreate(ctx, out, db); err != nil {
			return nil, err
		}
	}
	return out, nil
}

// DictDetailServiceDictDetailWithBeforeCreate called before DefaultCreateDictDetail in the default Create handler
type DictDetailServiceDictDetailWithBeforeCreate interface {
	BeforeCreate(context.Context, *gorm.DB) (*gorm.DB, error)
}

// DictDetailServiceDictDetailWithAfterCreate called before DefaultCreateDictDetail in the default Create handler
type DictDetailServiceDictDetailWithAfterCreate interface {
	AfterCreate(context.Context, *CreateDictDetailResponse, *gorm.DB) error
}

// Read ...
func (m *DictDetailServiceDefaultServer) Read(ctx context.Context, in *ReadDictDetailRequest) (*ReadDictDetailResponse, error) {
	db := m.DB
	if custom, ok := interface{}(in).(DictDetailServiceDictDetailWithBeforeRead); ok {
		var err error
		if db, err = custom.BeforeRead(ctx, db); err != nil {
			return nil, err
		}
	}
	res, err := DefaultReadDictDetail(ctx, &DictDetail{Id: in.GetId()}, db, in.Fields)
	if err != nil {
		return nil, err
	}
	out := &ReadDictDetailResponse{Result: res}
	if custom, ok := interface{}(in).(DictDetailServiceDictDetailWithAfterRead); ok {
		var err error
		if err = custom.AfterRead(ctx, out, db); err != nil {
			return nil, err
		}
	}
	return out, nil
}

// DictDetailServiceDictDetailWithBeforeRead called before DefaultReadDictDetail in the default Read handler
type DictDetailServiceDictDetailWithBeforeRead interface {
	BeforeRead(context.Context, *gorm.DB) (*gorm.DB, error)
}

// DictDetailServiceDictDetailWithAfterRead called before DefaultReadDictDetail in the default Read handler
type DictDetailServiceDictDetailWithAfterRead interface {
	AfterRead(context.Context, *ReadDictDetailResponse, *gorm.DB) error
}

// Update ...
func (m *DictDetailServiceDefaultServer) Update(ctx context.Context, in *UpdateDictDetailRequest) (*UpdateDictDetailResponse, error) {
	var err error
	var res *DictDetail
	db := m.DB
	if custom, ok := interface{}(in).(DictDetailServiceDictDetailWithBeforeUpdate); ok {
		var err error
		if db, err = custom.BeforeUpdate(ctx, db); err != nil {
			return nil, err
		}
	}
	if in.GetGerogeriGegege() == nil {
		res, err = DefaultStrictUpdateDictDetail(ctx, in.GetPayload(), db)
	} else {
		res, err = DefaultPatchDictDetail(ctx, in.GetPayload(), in.GetGerogeriGegege(), db)
	}
	if err != nil {
		return nil, err
	}
	out := &UpdateDictDetailResponse{Result: res}
	if custom, ok := interface{}(in).(DictDetailServiceDictDetailWithAfterUpdate); ok {
		var err error
		if err = custom.AfterUpdate(ctx, out, db); err != nil {
			return nil, err
		}
	}
	return out, nil
}

// DictDetailServiceDictDetailWithBeforeUpdate called before DefaultUpdateDictDetail in the default Update handler
type DictDetailServiceDictDetailWithBeforeUpdate interface {
	BeforeUpdate(context.Context, *gorm.DB) (*gorm.DB, error)
}

// DictDetailServiceDictDetailWithAfterUpdate called before DefaultUpdateDictDetail in the default Update handler
type DictDetailServiceDictDetailWithAfterUpdate interface {
	AfterUpdate(context.Context, *UpdateDictDetailResponse, *gorm.DB) error
}

// UpdateSet ...
func (m *DictDetailServiceDefaultServer) UpdateSet(ctx context.Context, in *UpdateSetDictDetailRequest) (*UpdateSetDictDetailResponse, error) {
	if in == nil {
		return nil, errors.NilArgumentError
	}

	db := m.DB

	if custom, ok := interface{}(in).(DictDetailServiceDictDetailWithBeforeUpdateSet); ok {
		var err error
		if db, err = custom.BeforeUpdateSet(ctx, db); err != nil {
			return nil, err
		}
	}

	res, err := DefaultPatchSetDictDetail(ctx, in.GetObjects(), in.GetMasks(), db)
	if err != nil {
		return nil, err
	}

	out := &UpdateSetDictDetailResponse{Results: res}

	if custom, ok := interface{}(in).(DictDetailServiceDictDetailWithAfterUpdateSet); ok {
		var err error
		if err = custom.AfterUpdateSet(ctx, out, db); err != nil {
			return nil, err
		}
	}

	return out, nil
}

// DictDetailServiceDictDetailWithBeforeUpdateSet called before DefaultUpdateSetDictDetail in the default UpdateSet handler
type DictDetailServiceDictDetailWithBeforeUpdateSet interface {
	BeforeUpdateSet(context.Context, *gorm.DB) (*gorm.DB, error)
}

// DictDetailServiceDictDetailWithAfterUpdateSet called before DefaultUpdateSetDictDetail in the default UpdateSet handler
type DictDetailServiceDictDetailWithAfterUpdateSet interface {
	AfterUpdateSet(context.Context, *UpdateSetDictDetailResponse, *gorm.DB) error
}

// List ...
func (m *DictDetailServiceDefaultServer) List(ctx context.Context, in *ListDictDetailRequest) (*ListDictDetailResponse, error) {
	db := m.DB
	if custom, ok := interface{}(in).(DictDetailServiceDictDetailWithBeforeList); ok {
		var err error
		if db, err = custom.BeforeList(ctx, db); err != nil {
			return nil, err
		}
	}
	pagedRequest := false
	if in.GetPaging().GetLimit() >= 1 {
		in.Paging.Limit++
		pagedRequest = true
	}
	res, err := DefaultListDictDetail(ctx, db, in.Filter, in.OrderBy, in.Paging, in.Fields)
	if err != nil {
		return nil, err
	}
	var resPaging *query.PageInfo
	if pagedRequest {
		var offset int32
		var size int32 = int32(len(res))
		if size == in.GetPaging().GetLimit() {
			size--
			res = res[:size]
			offset = in.GetPaging().GetOffset() + size
		}
		resPaging = &query.PageInfo{Offset: offset}
	}
	out := &ListDictDetailResponse{Results: res, PageInfo: resPaging}
	if custom, ok := interface{}(in).(DictDetailServiceDictDetailWithAfterList); ok {
		var err error
		if err = custom.AfterList(ctx, out, db); err != nil {
			return nil, err
		}
	}
	return out, nil
}

// DictDetailServiceDictDetailWithBeforeList called before DefaultListDictDetail in the default List handler
type DictDetailServiceDictDetailWithBeforeList interface {
	BeforeList(context.Context, *gorm.DB) (*gorm.DB, error)
}

// DictDetailServiceDictDetailWithAfterList called before DefaultListDictDetail in the default List handler
type DictDetailServiceDictDetailWithAfterList interface {
	AfterList(context.Context, *ListDictDetailResponse, *gorm.DB) error
}

// Delete ...
func (m *DictDetailServiceDefaultServer) Delete(ctx context.Context, in *DeleteDictDetailRequest) (*DeleteDictDetailResponse, error) {
	db := m.DB
	if custom, ok := interface{}(in).(DictDetailServiceDictDetailWithBeforeDelete); ok {
		var err error
		if db, err = custom.BeforeDelete(ctx, db); err != nil {
			return nil, err
		}
	}
	err := DefaultDeleteDictDetail(ctx, &DictDetail{Id: in.GetId()}, db)
	if err != nil {
		return nil, err
	}
	out := &DeleteDictDetailResponse{}
	if custom, ok := interface{}(in).(DictDetailServiceDictDetailWithAfterDelete); ok {
		var err error
		if err = custom.AfterDelete(ctx, out, db); err != nil {
			return nil, err
		}
	}
	return out, nil
}

// DictDetailServiceDictDetailWithBeforeDelete called before DefaultDeleteDictDetail in the default Delete handler
type DictDetailServiceDictDetailWithBeforeDelete interface {
	BeforeDelete(context.Context, *gorm.DB) (*gorm.DB, error)
}

// DictDetailServiceDictDetailWithAfterDelete called before DefaultDeleteDictDetail in the default Delete handler
type DictDetailServiceDictDetailWithAfterDelete interface {
	AfterDelete(context.Context, *DeleteDictDetailResponse, *gorm.DB) error
}

// DeleteSet ...
func (m *DictDetailServiceDefaultServer) DeleteSet(ctx context.Context, in *DeletesDictDetailRequest) (*DeleteDictDetailResponse, error) {
	db := m.DB
	objs := []*DictDetail{}
	for _, id := range in.Ids {
		objs = append(objs, &DictDetail{Id: id})
	}
	if custom, ok := interface{}(in).(DictDetailServiceDictDetailWithBeforeDeleteSet); ok {
		var err error
		if db, err = custom.BeforeDeleteSet(ctx, db); err != nil {
			return nil, err
		}
	}
	err := DefaultDeleteDictDetailSet(ctx, objs, db)
	if err != nil {
		return nil, err
	}
	out := &DeleteDictDetailResponse{}
	if custom, ok := interface{}(in).(DictDetailServiceDictDetailWithAfterDeleteSet); ok {
		var err error
		if err = custom.AfterDeleteSet(ctx, out, db); err != nil {
			return nil, err
		}
	}
	return out, nil
}

// DictDetailServiceDictDetailWithBeforeDeleteSet called before DefaultDeleteSetDictDetail in the default DeleteSet handler
type DictDetailServiceDictDetailWithBeforeDeleteSet interface {
	BeforeDeleteSet(context.Context, *gorm.DB) (*gorm.DB, error)
}

// DictDetailServiceDictDetailWithAfterDeleteSet called before DefaultDeleteSetDictDetail in the default DeleteSet handler
type DictDetailServiceDictDetailWithAfterDeleteSet interface {
	AfterDeleteSet(context.Context, *DeleteDictDetailResponse, *gorm.DB) error
}
